package com.realbrand.net.realbrandpay.ui.activity.wallet.set;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.base.BaseActivity;
import com.realbrand.net.realbrandpay.inter.IChangePasswd;
import com.realbrand.net.realbrandpay.ui.activity.login.LoginActivity;
import com.realbrand.net.realbrandpay.api.HTTPAPI;
import com.realbrand.net.realbrandpay.untils.Static;
import com.realbrand.net.realbrandpay.zxing.util.L;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.HTTP;

/**
 * Created by Administrator on 2017/9/11.
 */

public class PasswdChangeActivity extends BaseActivity {
    @BindView(R.id.passwdChangeActivity_toolbar)
    Toolbar passwdChangeActivityToolbar;
    @BindView(R.id.passwdChangeActivity_et_newPasswd)
    EditText passwdChangeActivityEtNewPasswd;
    @BindView(R.id.passwdChangeActivity_et_newPasswdAgain)
    EditText passwdChangeActivityEtNewPasswdAgain;
    @BindView(R.id.passwdChangeActivity_bt_next)
    Button passwdChangeActivityBtNext;

    //用户名
    private String userName;

    private String urlEncoder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        passwdChangeActivityToolbar.setTitle("");
        passwdChangeActivityToolbar.setNavigationIcon(R.mipmap.ic_back);
        setSupportActionBar(passwdChangeActivityToolbar);
        passwdChangeActivityToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(PasswdChangeActivity.this, PasswordManageActivity.class));
                finish();
            }
        });
        //获取用户名
        SharedPreferences userMassage = getSharedPreferences("userMassage", MODE_PRIVATE);
        userName = userMassage.getString("account", "");
        setOnClick();

    }

    @Override
    protected int getLayoutId() {
        return R.layout.passwdchange_activity;
    }

    @Override
    protected void setOnClick() {
        passwdChangeActivityBtNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.passwdChangeActivity_bt_next:
                //先获取用户两次输入的新密码
                String newPasswdOne = passwdChangeActivityEtNewPasswd.getText().toString();
                String newPasswdTwo = passwdChangeActivityEtNewPasswdAgain.getText().toString();
                //判断用户有没有输入新密码
                if (TextUtils.isEmpty(newPasswdOne) || TextUtils.isEmpty(newPasswdTwo)) {
                    Static.toast(this, "请输入新密码。。。");
                } else {
                    if (Static.isLetterDigit(newPasswdOne) && Static.passwdLength(newPasswdOne)) {
                        //判断两次输入的新密码是否一致
                        if (newPasswdOne.equals(newPasswdTwo)) {
                            //做插入请求
                            changePasswd(newPasswdOne);
                        } else {
                            Static.toast(this, "两次输入的密码不一致，请重新输入。。。");
                        }
                    } else {
                        Static.toast(this, "请确保密码是6-12位的同时带有数字跟字母。。");
                    }
                }
                break;
        }
    }

    private void changePasswd(String newPasswdOne) {
        //先将参数放入数组然后进行urlencoder编码
        ArrayList<String> keyList = new ArrayList<>();
        keyList.add("UserAccount");
        keyList.add("NewPasswd");
        ArrayList<String> valueList = new ArrayList<>();
        valueList.add(userName);
        valueList.add(newPasswdOne);
        String string = Static.getString(keyList, valueList);
        try {
            urlEncoder = URLEncoder.encode(string, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("SOURCE", "PasswdChangeActivity");
            jsonObject.put("CREDENTIALS", "0");
            jsonObject.put("TERMINAL", "1");
            jsonObject.put("INDEX", Static.getDay());
            jsonObject.put("METHOD", "ModifyPasswd");
            jsonObject.put("DATA", urlEncoder);
            RequestBody body = RequestBody.create(HTTPAPI.JSON, jsonObject.toString());
            Retrofit retrofit = new Retrofit.Builder().baseUrl(HTTPAPI.BASE_URL).build();
            IChangePasswd iChangePasswd = retrofit.create(IChangePasswd.class);
            Call<ResponseBody> call = iChangePasswd.changePasswd(body);
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    if (response == null) {
                        Static.toast(PasswdChangeActivity.this, "网络繁忙。。。");
                    } else {
                        try {
                            String result = response.body().string();
                            if (result.equals("")) {
                                Static.toast(PasswdChangeActivity.this, "网络繁忙。。。");
                            } else {
                                L.d(L.TAG, "修改密码返回的结果" + result);
                                try {
                                    JSONObject object = new JSONObject(result);
                                    JSONArray data = object.getJSONArray("DATA");
                                    JSONObject dataJSONObject = data.getJSONObject(0);
                                    String dataString = dataJSONObject.getString("result");
                                    if ("1".equals(dataString)) {
                                        //表示修改成功
                                        startActivity(new Intent(PasswdChangeActivity.this, LoginActivity.class));
                                        finish();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
